package com.kwin.test;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * @author : Kwin
 * @version V1.0
 * @Package com.kwin.test
 * @Description:
 * @date 2021年06月11日 10:20
 **/
public class MybatisPlusGenerator {
    public static void main(String[] args) {
        new MybatisPlusGenerator().generator();
    }
    
    public static final String database = "kwin";
    public static final String user = "root";
    public static final String passwd = "Greentree";
    //要生成的表名
    private static String[] tables= {"user"};
    private static final String packageInfo = "com.kwin.test";
    public void generator(){
        AutoGenerator autoGenerator = new AutoGenerator();
        /**
         * 全局配置
         */
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir("/src/main/java");
        globalConfig.setAuthor("kwin");
        globalConfig.setOpen(false);
        globalConfig.setActiveRecord(true);
        globalConfig.setFileOverride(true);
        globalConfig.setMapperName("%sMapper");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setControllerName("%sController");
        autoGenerator.setGlobalConfig(globalConfig);
        /**
         * 数据源配置
         */
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://10.100.96.218:3306/"+database+"?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8");
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUsername(user);
        dataSourceConfig.setPassword(passwd);
        autoGenerator.setDataSource(dataSourceConfig);
        /**
         * 策略配置，生成的表在这里配置
         */
        StrategyConfig stConfig = new StrategyConfig();
        stConfig.setCapitalMode(true);
        stConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        stConfig.setNaming(NamingStrategy.underline_to_camel);
        
        stConfig.setInclude(tables);
        stConfig.setRestControllerStyle(true);//@restController
        stConfig.setEntityLombokModel(true);//@lombook
        autoGenerator.setStrategy(stConfig);
        /**
         * 生成文件路径配置
         */
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent(packageInfo);
/*        packageConfig.setController("controller");
        packageConfig.setMapper("mapper");
        packageConfig.setService("serviceTemp");
        packageConfig.setServiceImpl("serviceTemp.Impl");*/
        packageConfig.setEntity("entity.po");
        autoGenerator.setPackageInfo(packageConfig);
        
        autoGenerator.execute();
    }
}
